import sys

class Template(object):
    def __init__(self):
        """ Default values """
        # Label
        self.label          = ""
        self.hidelabel      = False
        self.activelabel    = False
        # Bandwidth
        self.maxuploads     = None
        self.uploadrate     = None
        self.downloadrate   = None
        # Queue
        self.uploadtime     = None
        self.uploadratio    = None
        self.distimeouts    = False
        # Trigger
        self.trigger        = False
        self.triggermatch   = {"Tracker": "", "Torrent": "", "TorrentFile": "", "File": ""}
        
    def load(self, args):
        """ load Template info """
        assert (len(args) == 11)
        self.label, self.hidelabel, self.activelabel, self.maxuploads, self.uploadrate, self.downloadrate, \
                    self.uploadtime, self.uploadratio, self.distimeouts, self.trigger, self.triggermatch = args
        assert(type(self.triggermatch) == type({}))

    def get(self):
        """ get Template info """
        return [self.label, self.hidelabel, self.activelabel, self.maxuploads, self.uploadrate, self.downloadrate,
                self.uploadtime, self.uploadratio, self.distimeouts, self.trigger, self.triggermatch]

    def copy(self, template):
        """ copy Template info from other template """
        self.load(template.get())
        
    def isTriggerActive(self):
        """ returns true in case trigger rule active """
        return self.trigger and filter(lambda x:bool(x), self.triggermatch.values())
    
class LabelManager(object):
    """
    Manage all Templates
    """
    def __init__(self):
        self.d_templates = {}   # {template_name: template_object}
        self.l_templates = []   # [template_name,] - by trigger order

        try:
            self.load()
        except:
            pass
        
    def get(self, templateName):
        return self.d_templates.get(templateName)

    def new(self, name):
        t = Template()
        t.label = name
        self.add(t)
        
    def add(self, template):
        self.d_templates[template.label] = template
        self.l_templates.append(template.label)
        
    def remove(self, templateName):
        del self.d_templates[templateName]
        self.l_templates.remove(templateName)

    def rename(self, oldTemplateName, newTemplateName):
        # Replace in dict
        template = self.d_templates[oldTemplateName]
        del self.d_templates[oldTemplateName]
        self.d_templates[newTemplateName] = template
        # Replace in trigger list
        index = self.l_templates.index(oldTemplateName)
        self.l_templates.remove(oldTemplateName)
        self.l_templates.insert(index, newTemplateName)

    def reorder(self, newlist):
        assert len(newlist) == len(self.l_templates)
        self.l_templates = newlist

    def load(self):
        data = eval(utility.config.Read("templates"))
        for item in data:
            template = Template()
            try:
                template.load(item)
            except:
                pass
            else:
                self.add(template)
                
    def save(self):
        templates = [self.d_templates[templateName].get() for templateName in self.l_templates]
        utility.config.Write("templates", templates)
        utility.config.Flush()
